package com.ln.第一遍1105.demo01;



import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author Ln
 * @version 终极版
 * @date 2022/11/5 15:13
 */
public class Test01 {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("欧阳雪",18,"中国",'F'));
        personList.add(new Person("Tom",24,"美国",'M'));
        personList.add(new Person("Harley",22,"英国",'F'));
        personList.add(new Person("向天笑",20,"中国",'M'));
        personList.add(new Person("李康",22,"中国",'M'));
        personList.add(new Person("小梅",20,"中国",'F'));
        personList.add(new Person("何雪",21,"中国",'F'));
        personList.add(new Person("李康",22,"中国",'M'));
        //筛选出年龄大于20
//        personList.stream().filter(t->t.getAge()>20).forEach(System.out::println);

        //map映射操作--名称
//        personList.stream().map(t->t.getName()).forEach(System.out::println);

        //sort排序操作--年龄
//        personList.stream().sorted(((o1, o2) -> o1.getAge()-o2.getAge())).forEach(System.out::println);
//        personList.stream().sorted((o1,o2)->Integer.compare(o1.getAge(),o2.getAge())).forEach(System.out::println);


        //find操作-- findFirst 获取流中第一个元素
//        Optional<Person> first = personList.stream().findFirst();
//        System.out.println(first);

        //find操作---findAny() 获取流中任意一个元素  必须使用并行流测试
//        Optional<Person> any = personList.stream().findAny();
//        System.out.println(any);


        //匹配操作---allMatch,anyMatch--所有元素都必须匹配条件 才返回true
//        boolean b = personList.stream().allMatch(t -> t.getAge() > 20);
//        System.out.println(b);
//
//        boolean b1 = personList.stream().anyMatch(t -> t.getAge() > 23);
//        System.out.println(b1);


        // 聚合操作 max  min count个数
//        Optional<Person> max = personList.stream().max((o1, o2) -> o1.getAge() - o2.getAge());
//        System.out.println(max.get());
//
//        Optional<Person> min = personList.stream().min((o1, o2) -> o1.getAge() - o2.getAge());
//        System.out.println(min.get());
//
//        long count = personList.stream().count();
//        System.out.println(count);


    }
}
class Person {
    private String name;
    private Integer age;
    private String country;
    private char sex;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", country='" + country + '\'' +
                ", sex=" + sex +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public Person(String name, Integer age, String country, char sex) {
        this.name = name;
        this.age = age;
        this.country = country;
        this.sex = sex;
    }
}
